Understand How Handle C++ Compiler Warning Levels
Code is compiled with no warnings at warning level 4. Exceptions are detailed below. Compiling 3rd-party Code Code that is compiled by us but does not belong to us does not have to conform to our coding standards, and thus does not need to comply with this rule. Examples of this include the Microsoft system header filers, STL, other libraries which have purchased, etc. The warnings can be turned of temporarily like so: #pragma warning( push, 1 ) #include #pragma warning( pop ) Disabling Warnings "#pragma warning" may be used to disable unavoidable warnings. Two categories of warnings that are okay to disable are informational warnings, such as the compiler indicating that a function is selected to be inlined, and warnings dealing with identification of non-standard C++ usage, which is okay because all Windows code uses “Microsoft Extensions” to C++ and is common. The table below lists warnings that are okay to disable and are commonly disabled. There may be other warnings that fit in the above specified categories that are okay to disable but are not listed here. If one is found, please notify the author for inclusion in this list. All of these warnings can be turned off by including the following header file in the projects stdafx.h file: clearcase:\BuildSupport\Wonderware\Compiles\DisableWarnings.h Warning which can be turned off: *C4001 – Nonstandard extension 'single line comment' was used *C4049 – Compiler limit : terminating line number emission. The file contains more than 65,536 (64K) source lines *C4152 – Non standard extension, function/data ptr conversion in expression *C4200 – Nonstandard extension used : zero-sized array in struct/union *C4201 – Nonstandard extension used : nameless struct/union *C4204 – Nonstandard extension used : non-constant aggregate initializer *C4211 – Nonstandard extension used : redefined extern to static *C4213 – Nonstandard extension used : cast on l-value *C4214 – Nonstandard extension used : bit field types other than int *C4238 – Nonstandard extension used : bit field types other than int *C4239 – Nonstandard extension used : 'token' : conversion from 'type' to 'type' *C4290 – C++ exception specification ignored except to indicate a function is not __declspec(nothrow) *C4503 – Decorated name length exceeded, name was truncated *C4505 – Unreferenced local function has been removed *C4514 – Unreferenced inline function has been removed *C4699 – This warning provides informational messages about the status of precompiled headers *C4711 – Function selected for inline expansion *C4786 – Identifier was truncated to 'number' characters in the debug information Fixing Code to Remove Warnings All warnings other than the ones listed above cannot be turned off. The table below lists some of these warnings and how to fix the code so that the warnings don’t show up. See the comment next to each warning on suggested ways to modify code to remove the warning. Fixing Code to Remove Warnings Warning How to correct code C4005 – Macro redefinition This is probably happening because a particular header file that defines a macro is somehow getting #included recursively. Make sure header files are guarded with #pragma once C4018 – Signed/unsigned mismatch. This is probably a software bug. Care is taken when signed and unsigned types are used in the same expression or comparison. It’s very easy to get the wrong results on larger numbers. For instance, and short integer value of -5 will change to number 65531 if implicitly converted to type of short unsigned integer C4065 – Switch statement contains 'default' but no 'case' labels This may happen with code created by a code generation such as yacc or bison. Correct the input files to avoid this warning. C4067 – Unexpected tokens following preprocessor directive - expected a newline The main reason one would get this error is because C++ used to allow arbitrary text after a preprocessor directives, such as: #ifdef _DEBUG ... #endif _DEBUG The text after the #endif is now illegal. To fix, change code to look like this: #ifdef _DEBUG ... #endif // _DEBUG C4081 – Expected 'token1'; found 'token2' See message C4097 – Expected pragma parameter to be 'token1' or 'token2' See message C4100 – Unreferenced formal parameter There are several ways to fix this: · Comment out the name of the variable in the parameter list · Reference the variable by itself after the return statement: … return; param1; // empty reference } C4101 – Unreferenced local variable It’s best to just remove the variable. If it’s there because it’s used by some other code that’s currently commented out, remove the variable and code that’s commented out. If the variable is there to view its result in the debugger, then you can disable this particular instance of the warning by referencing the variable immediately after the declaration: short objectId = 0; objectId; // for debugging C4102 – Unreferenced label Remove the label C4115 – Named type definition in parentheses This can happen if an undefined type is used in a function prototype. The compiler thinks you’re defining variable inside of parenthesis and will generate this warning. Check your #include list. C4127 – Conditional expression is constant This can happen with some 3rd party libraries, but can also happen if we use similar tricks that expand to compile time constants for conditions. C4146 – Unary minus operator applied to unsigned type, result still unsigned Should never happen in our own code. If it happens in 3rd party code, disable it. C4189 – Local variable is initialized by not referenced (see comments for C4101) C4192 – Automatically excluding 'name' while importing type library 'library' Use no_auto_exclude and include(…) #import attributes to turn of automatic exclusion of duplicates and specify names explicitly C4244 – Conversion from 'type1' to 'type2', possible loss of data If the lost of data is okay, then use static_cast to explicitly cast the data to the correct type. C4245 – Conversion from 'type1' to 'type2', signed/unsigned mismatch See C4018. Make sure you understand how the data is going to get converted then use static_cast to explicitly cast the data to match types. C4273 – Function' : inconsistent DLL linkage Use consistent dllimport qualifier __declspec. C4284 – Return type for 'identifier::operator –>' is not a UDT or reference to a UDT. Will produce errors if applied using infix notation There’s probably a syntax error here. Check to ensure that the -> operator is being used correctly. Is the identifier on the left side valid? C4310 – Cast truncates constant value Your using the wrong data type with the constant value. Either pick constant in range or change the data type. C4506 – No definition for inline function 'function' See message C4510 – default constructor could not be generated Create an explicit default constructor C4511 – copy constructor could not be generated Create an explicit copy constructor C4512 – assignment operator could not be generated Create an explicit assignment operator C4530 – C++ exception handler used, but unwind semantics are not enabled. Specify /Ehsc Enable exception handling for the project. C4610 – Object 'class' can never be instantiated - user-defined constructor required Create an explicit constructor C4663 – To explicitly specialize a template class, use the new syntax. Add template<> in front of the class declaration for an explicitly specialized class template. C4700 – Local variable used without having been initialized Be careful with this warning. It could actually point to a software defect. The only time this is valid to have is when a variable is passed by reference to a function. The compiler doesn’t know that it’s okay for the input variable to not be initialized (C++ allows this.) To remove this warning, initialize the variable before using it. C4701 – Local variable 'name' may be used without having been initialized See C4700 C4702 – Unreachable code Remove the dead code, or if the code isn’t dead, fix the control paths. C4706 – Assignment within conditional expression Assignments within conditional expressions should not be made. Fix the code. C4710 – Function not inlined Remove the inline keyword. C4715 – Not all control paths return a value If the compiler is correct, this usually indicates a software defect and the code needs to be fixed. The compiler is sometimes wrong. For instance, it may not know that a function you are calling never returns (such as ThrowError().) Simply change the code to throw anything immediately after, or simply return. C4800 – Forcing value to bool 'true' or 'false' (performance warning) This happens when trying to convert a BOOL (or int) to C++ bool. Example: bool flag = fFlag; Doing so is a performance issue so the compiler won’t let you even cast to bool. To fix, do this: bool flag = fFlag != 0; C4996 – ‘function': was declared deprecated Must not use deprecated functions unless explicitly identified in the project development plan as to why. Warning of Unfinished Code Do not use compiler warnings as a way to indicate and track code that needs to be finished. Builds cannot have Warnings in them. Instead, to remind yourself of unfinished code, use #pragma message. Category:Cheat